//package com.sicong.common.security.filter;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.sicong.admin.entity.User;
//import com.sicong.common.security.entity.SecurityUser;
//import com.sicong.common.security.utils.TokenUtil;
//import com.sicong.common.core.enums.ResultEnum;
//import com.sicong.common.core.utils.ResponseUtil;
//import com.sicong.common.core.utils.ResultUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpMethod;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.AuthenticationServiceException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.ArrayList;
//
//public class TokenLoginFilter extends UsernamePasswordAuthenticationFilter {
//
//    private final TokenUtil tokenUtil;
//
//    private final RedisTemplate<String,Object> redisTemplate;
//
//    private final AuthenticationManager authenticationManager;
//
//    public TokenLoginFilter(TokenUtil tokenUtil, RedisTemplate<String, Object> redisTemplate, AuthenticationManager authenticationManager) {
//        this.tokenUtil = tokenUtil;
//        this.redisTemplate = redisTemplate;
//        this.authenticationManager = authenticationManager;
//        this.setPostOnly(false);
//        this.setRequiresAuthenticationRequestMatcher(new AntPathRequestMatcher("/login","POST"));
//    }
//
//    /**
//     * 获取用户名和密码
//     */
//    @Override
//    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
//        if (!request.getMethod().equals(HttpMethod.POST.name())) {
//            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
//        } else {
//            String username = request.getParameter("username");
//            String password = request.getParameter("password");
//            return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username,password,
//                    new ArrayList<>()));
//        }
//    }
//
//    /**
//     * 认证成功
//     */
//    @Override
//    protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
//                                            FilterChain chain, Authentication authResult) throws IOException, ServletException {
//        SecurityUser user = (SecurityUser) authResult.getPrincipal();
//        // 生成token
//        String token = tokenUtil.createToken(user.getUser().getUsername());
//        // 将用户名称和权限列表放入redis
//        redisTemplate.opsForValue().set(user.getUser().getUsername(),user.getPermissions());
//        ResponseUtil.out(response, ResultUtil.success(token));
//    }
//
//    /**
//     * 认证失败
//     */
//    @Override
//    protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
//                                              AuthenticationException failed) throws IOException, ServletException {
//        ResponseUtil.out(response, ResultUtil.error(ResultEnum.ERROR_PASSWORD));
//    }
//
//}
